package com.zjb.datastructure.search;

import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class BinarySearch {

    static int search(int[] nums, int result) {

        int low = 0, high = nums.length;

        while (low < high) {

            int mid = (low + high) / 2;
            if (nums[mid] == result) {
                return mid;
            }
            if (nums[mid] > result) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }


        }
        return -1;
    }

    public static void main(String[] args) {
        int[] ints = IntStream.range(0, 10).toArray();
        for (int anInt : ints) {
            System.out.print(anInt+",");
        }
        System.out.println();
        int search = search(IntStream.range(0, 10).toArray(), 9);
        System.out.println(search);
         search = recursionSearch(IntStream.range(0, 10).toArray(), 9);
        System.out.println(search);



    }

    static int recursionSearch(int[] nums,int result) {
        return recursion(nums, 0, nums.length, result);

    }

   static int recursion(int[] nums,int low,int high,int result) {

        if (low > high) {
            return -1;
        }
        int mid = (low + high) / 2;
        if (nums[mid] == result) {
            return mid;
        } else if (nums[mid] > result) {
            high = mid - 1;
            return recursion(nums, low, high, result);
        } else {
            low = mid + 1;
            return recursion(nums, low, high, result);
        }

    }



}

































